package com.example.zqq.shoushipassword;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.zqq.shoushipassword.view.NinePointLineView;

/**
 * 重新设置手势密码
 */
public class ResatrtActivity extends AppCompatActivity {

    private LinearLayout nine_con;//九宫格容器
    private RelativeLayout mTitleLayout;
    private TextView showInfo;
    private TextView mName;
    private TextView mPhone;
    private TextView mBottom;

    private AlertDialog mServiceDialog;
    private NinePointLineView nv;//九宫格View

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resatrt);

        nv = new NinePointLineView(ResatrtActivity.this,2);
        findView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        savePwd();
    }

    private void findView() {
        mTitleLayout=(RelativeLayout)findViewById(R.id.title_layout);
        nine_con = (LinearLayout)findViewById(R.id.nine_con);
        showInfo=(TextView)findViewById(R.id.show_set_info);
        mBottom=(TextView)findViewById(R.id.bottom);
        mName=(TextView)findViewById(R.id.name);
        mPhone=(TextView)findViewById(R.id.phone);

        nine_con.addView(nv);
        mTitleLayout.setVisibility(View.VISIBLE);

        SharedPreferences setting = getSharedPreferences("userInfo", MODE_PRIVATE);
        String name = setting.getString("userName", "");
        if(name==null || name.equals(""))
            mName.setText("");
        else
            mName.setText("*"+name.substring(1));   //*强强
        if(setting.getString("userPhone", "")==null || setting.getString("userPhone", "").equals(""))
            mPhone.setText("");
        else        //136*****057
            mPhone.setText(setting.getString("userPhone", "").substring(0, 3)+"*****"+setting.getString("userPhone", "").substring(8));


        //忘记密码
        mBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CallDialog();
            }
        });


    }

    /**
     * 忘记密码弹窗
     */
    private void CallDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        mServiceDialog = builder.create();
        mServiceDialog.show();
        mServiceDialog.setContentView(R.layout.service_dialog);

        mServiceDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams params = mServiceDialog.getWindow().getAttributes();
        params.gravity = Gravity.CENTER;
        mServiceDialog.getWindow().setAttributes(params);

        TextView sure;
        TextView cancle;
        TextView phone;
        sure=(TextView)mServiceDialog.findViewById(R.id.sure);
        sure.setText("重新登录");
        cancle=(TextView)mServiceDialog.findViewById(R.id.cancle);
        phone=(TextView)mServiceDialog.findViewById(R.id.service_phone);
        phone.setText("忘记手势密码，需要重新登录");

        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mServiceDialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ResatrtActivity.this, PayPhoneNumActivity.class);

                startActivityForResult(intent, PHONENUM);
            }
        });
    }


    public final static int savePwd = 0x000003F0;
    public static int mNumberR = 0; // 用来记录手势密码输入错误的次数，最大为5

    public void savePwd(){
        SharedPreferences shareDate = getSharedPreferences("GUE_PWD", 0);
        boolean is_second_error = shareDate.getBoolean("SECOND_ERROR", false);

        if(is_second_error){
//            SmalltaurusApplication.mNumberR++;
            mNumberR++;
            if (mNumberR > 4) {
                Intent intent = new Intent(this, PayPhoneNumActivity.class);
                startActivityForResult(intent, savePwd);

            } else {
                showInfo.setTextColor(getResources().getColor(R.color.red));
                showInfo.setText("密码输入错误，还可以再输入" + (5 - mNumberR) + "次");
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 禁用Back键
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }


    public final int PHONENUM = 0x000000F4;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case PHONENUM:
                if (requestCode == savePwd) {
//                    SmalltaurusApplication.choise=1;
                    ResatrtActivity.this.finish();
                    return;
                }
//                mToken.clearToggle();
//                SmalltaurusApplication.choise=1;
                ResatrtActivity.this.finish();
                break;
            default:
                break;
        }
    }
}


